<?php
/**
 * 属性管理
 * @author cjli
 *
 */   
class AttributeAction extends HomeAction
{
	public function _initialize()
	{
		parent::_initialize();
	}
	
	public function attr_value_ajax()
	{
		global $language_id;
		$res = array(
			'error' => 0,
			'name' => null,
			'content' => null,
			'image' => null,
		);
		$aid = intval($_REQUEST['aid']);
		if($aid){
			$attribute_values_items_db = D('attribute_values_items');
			$attribute_values_items = $attribute_values_items_db->where(array('att_value_id' => $aid,'language_id' => $language_id))->find();
			//$as = $attribute_values_items_db->getLastSql();
			$attribute_values_db = D('attribute_values');
			$attribute_values_info = $attribute_values_db->find($aid);

			$res = array(
				'error' => 1,
				'name' => $attribute_values_items['attr_value'],
				'content' => $attribute_values_items['attr_content'],
				'image' => '<img src="'.$attribute_values_info['attr_img'].'">',
			);
		}
		
		echo json_encode($res);
	}
	
	public function attribute_info_ajax()
	{
		global $language_id;
		$res = array(
			'error' => 0,
			'name' => null,
			'content' => null,
		);
		$aid = intval($_REQUEST['aid']);
		if($aid){
			$attribute_items_db = D('attribute_items');
			$attribute_items_info = $attribute_items_db->where(array('attribute_id' => $aid,'language_id' => $language_id))->find();

			$res = array(
				'error' => 1,
				'name' => $attribute_items_info['name'],
				'content' => $attribute_items_info['attr_content'],
			);
		}
		
		echo json_encode($res);
	}
	
	public function attr_value_info($aid)
	{
		global $language_id;
		$res = array();
		if($aid){
			$attribute_values_items_db = D('attribute_values_items');
			$attribute_values_items_db->field('attr_value,attr_content');
			$attribute_values_items = $attribute_values_items_db->where(array('att_value_id' => $aid,'language_id' => $language_id))->find();
			//$as = $attribute_values_items_db->getLastSql();
			$attribute_values_db = D('attribute_values');
			$attribute_values_db->field('attr_img');
			$attribute_values_info = $attribute_values_db->find($aid);
			
			$res['name'] 	= $attribute_values_items['attr_value'];
			$res['content'] = $attribute_values_items['attr_content'];
			$res['image']  	= $attribute_values_info['attr_img'];
		}
		
		return $res;
	}
	
	/**
	 * 更改购物车的身型数据
	 */
	public function measure_value_ajax()
	{
		$result = array(
			'error' => 0,
			'message' => null,
            'error_chest'=>0,
            'error_measure'=>0,
		);
		
		$cid 		= intval($_REQUEST['cid']);
		$measure_id = intval($_REQUEST['measure_id']);
		$measureInfo = AttributeModel::get_user_measure_info($measure_id);
        $model_id = intval($_REQUEST['model_id']);
        if($model_id==27){
            if(!$measureInfo){
                $result = array(
                    'error' => 0,
                    'message' => L('without_the_shape_data'),
                );
            }else{
                if($measureInfo['m_status']!=10&&$measureInfo['m_status']!=5&&($measureInfo['chest']>C('chest_max')||$measureInfo['chest']<C('chest_min'))){
                    $result['error_chest']=1;
                }
            }
        }
        //身形数据是否完整
        if(C("data_".$model_id)&&$measureInfo['m_status']!=5&&$measureInfo['m_status']!=10){
            foreach (C("data_".$model_id ) as $v){
                if($measureInfo[$v]==0){
                    $result['error_measure']=1;
                }
            }
        }
		if(!$measureInfo){
			$result = array(
				'error' => 0,
				'message' => L('without_the_shape_data'),
			);
		}else{
			$cart_products_db = D('cart_products');
			$data = array(
				'measure_id' => $measure_id,
			);
			$id=$cart_products_db->where(array('id' => $cid))->save($data);
            if($id){
                //判断购物车是否有羊绒衫  并且胸围是否在范围内
                $cartList =CartModel::get_cart_product_list();
                $i=0;//用于标记羊绒衫于身形数据是否相符
                $j=0;//用于标记身形数据好似否完整
                foreach ($cartList['productList'] as &$cart){
                    $measure_info=AttributeModel::get_user_measure_info($cart['measure_id']);
                    if($cart['product']['model_id']==27){
                        //获取胸围
                        if(($measure_info['chest']>C('chest_max')||$measure_info['chest']<C('chest_min'))&&$measure_info['m_status']!=5&&$measure_info['m_status']!=10){
                            $i++;
                        }
                    }
                    if(C("data_".$cart['product']["model_id"])&&$measure_info['m_status']!=5&&$measure_info['m_status']!=10){
                        $data=C("data_".$cart['product']["model_id"]);
                        foreach ($data as $v){
                            if($measure_info[$v]==0){
                                $j++;
                            }
                        }
                    }
                }
                if($i>0){
                    $result['chest']=115;
                }else{
                    $result['chest']=0;
                }
                if($j>0){
                    $result['data']=1;
                }else{
                    $result['data']=0;
                }
            }
			
			$result['message'] = $measureInfo['measure_name'];
			$result['error'] = '1';
		}
		
		echo json_encode($result);
	}
	/**
	 * 定制的身形数据（默认）
	 */
	public function attribute_list()
	{
		global $language_id;
		$page = $_REQUEST['page'];
		$pid = $_REQUEST['pid'];
		
		$result = array(
			'error' => 0,
			'message1' => null,
			'message2' => null,
		);
		
		//产品属性
		$attributeList = ProductModel::get_product_custom_attribte_list($pid, array('attr_sell' => 0),$language_id);// 获得商品的规格和属性
		//dump($attributeList);exit;
		$count_attributeList = count($attributeList)-4;
		//选择了的属性
		if(session('custom')){
			$customArray = unserialize(base64_decode(session('custom')));
		}

		if($attributeList && ( !( isset($customArray) && $customArray[$pid]) ) ){
//				if($attributeList){
			$attrSelect = array();
			foreach ($attributeList as $attr_id => $attr){
				foreach ($attr['values'] as $attrv){
					if($attr['default'] == $attrv['product_attr_id']){
						$attrSelect[$attr_id] = $attr_id.':'.$attr['attr_name'].':'.$attrv['product_attr_id'] .':'. $attrv['label'] .':'. $attrv['attr_value_id'];
						break;
					}
				}
			}
			$custom_cookie = base64_encode(serialize(array($pid => $attrSelect)));
			session('custom', $custom_cookie);
		}
		elseif( $customArray[$pid] ){
			$attrSelect = array();
			foreach ($customArray[$pid] as $attr_id => $pattrArr){
				$attrArr = explode(':', $pattrArr);
				if($attrArr[2]==''){
					$attributeList[$attr_id]['default_value'] = $attrArr[3];
				}else{
					$attributeList[$attr_id]['default'] = $attrArr[2];
				}
			}
		}
//		dump($attributeList);exit;
		
		$message1 = '';
		$message2 = '';
		if($page == 1){
			$i=0;
			foreach ($attributeList as $attr_id => $attr){ 
				$i++;
				if($i<=3){
					$j=0;
					if(isset($attr['values']) && $attr['values']){
						if($message1 == ''){
							$message1 = $i;
						}else{
							$message1 .= ','.$i;
						}
						foreach ($attr['values'] as $attrv){
							$j++;
							if($attr['default']== $attrv['product_attr_id']){
								if($message2 == ''){
									$message2 = $j;
								}else{
									$message2 .= ','.$j;
								}
							}
						}
					}
				}
			}
		}elseif($page == 2){
			$i=0;
			$k=0;
			foreach ($attributeList as $attr_id => $attr){ 
				$i++;
				if($i>3 && $i<=6){
					$k++;
					$j=0;
					if(isset($attr['values']) && $attr['values']){
						if($message1 == ''){
							$message1 = $k;
						}else{
							$message1 .= ','.$k;
						}
						foreach ($attr['values'] as $attrv){
							$j++;
							if($attr['default']== $attrv['product_attr_id']){
								if($message2 == ''){
									$message2 = $j;
								}else{
									$message2 .= ','.$j;
								}
							}
						}
					}
				}
			}
		}elseif($page == 3){
			$i=0;
			$k=0;
			foreach ($attributeList as $attr_id => $attr){ 
				$i++;
				if($i>6 && $i<=9){
					$k++;
					if($i< 9){
						$j=0;
						if(isset($attr['values']) && $attr['values']){
							if($message1 == ''){
								$message1 = $k;
							}else{
								$message1 .= ','.$k;
							}
							foreach ($attr['values'] as $attrv){
								$j++;
								if($attr['default']== $attrv['product_attr_id']){
									if($message2 == ''){
										$message2 = $j;
									}else{
										$message2 .= ','.$j;
									}
								}
							}
						}
					}
				}
			}
		}elseif($page==4){
			$i=0;
			$k=0;
			foreach ($attributeList as $attr_id => $attr){ 
				$i++;
				if($i>12 && $i<=13){
					$k++;
					$j=0;
					if(isset($attr['values']) && $attr['values']){
						if($message1 == ''){
							$message1 = $k;
						}else{
							$message1 .= ','.$k;
						}
						foreach ($attr['values'] as $attrv){
							$j++;
							if($attr['default']== $attrv['product_attr_id']){
								if($message2 == ''){
									$message2 = $j;
								}else{
									$message2 .= ','.$j;
								}
							}
						}
					}
				}
			}
		}elseif($page==5){
			$i=0;
			$k=0;
			foreach ($attributeList as $attr_id => $attr){ 
				$i++;
				if($i>13 && $i<=16){
					$k++;
					$j=0;
					if(isset($attr['values']) && $attr['values']){
						if($message1 == ''){
							$message1 = $k;
						}else{
							$message1 .= ','.$k;
						}
						foreach ($attr['values'] as $attrv){
							$j++;
							if($attr['default']== $attrv['product_attr_id']){
								if($message2 == ''){
									$message2 = $j;
								}else{
									$message2 .= ','.$j;
								}
							}
						}
					}
				}
			}
		}
		$result = array(
			'error' => 1,
			'message1' => $message1,
			'message2' => $message2,
		);
		
		echo json_encode($result);
	}
	
}